var lineId;
var lineProtects = [];
//key是保护名，value是动作绝对时间的数组
var actTimes = new Map();
//key是保护名，value是动作时间的数组
var actTimelags = new Map();
//key是保护名，value是通道名数组
var chanels = new Map();
//key是通道名，value是通道告警信息数组
var alarms = new Map();


function getData(){
	 $.getJSON("/rest/protect/getProtects/"+lineId,function(result){
		// [[mid,name,pt_id]]
		lineProtects = result;
		//为两个map加key
		for(var i in lineProtects){
			var protectName = lineProtects[i][1];
			actTimes.put(protectName, []);
			actTimelags.put(protectName, []);
			chanels.put(protectName, []);
		}
		initAction();
		initAlarms();
	});
}
function initAction(){
	 $.getJSON('/rest/protect/getProtectActsByLineId/'+lineId,function(result){
		//[[mRID,protectName,timeLag,actTime]]
		var data = result;
		var i=0;
		for(i in data){
			var protectname = data[i][1];
			var timelag = data[i][2];
			var time = new Date(data[i][3]).toLocaleString();
			actTimelags.get(protectname).push(timelag);
			actTimes.get(protectname).push(time);
		}
		initChart1();
		initChart2();
	});
}
function initAlarms(){
	 $.getJSON('/rest/protect/getProtectAlarmsByLineId/'+lineId,function(result){
			//[[ mProtectName,level,trigger_time,alarm_channel,name]]
			chanelAlarms = result;
			var j=0;
			for(j in chanelAlarms){
				var protectName = chanelAlarms[j][0];
				//搜出所有的通道名
				var chanelName =  chanelAlarms[j][3];
				if(chanels.get(protectName).indexOf(chanelName) == -1){
					chanels.get(protectName).push(chanelName);
				}
				//每个通道都加入
				if(alarms.containsKey(chanelName)){
					alarms.get(chanelName).push({
						level:chanelAlarms[j][1],
						triggerTime:chanelAlarms[j][2],
						name:chanelAlarms[j][4],
						chanelName:chanelName
					});
				}else{
					alarms.put(chanelName,[{
						level:chanelAlarms[j][1],
						triggerTime:chanelAlarms[j][2],
						name:chanelAlarms[j][4],
						chanelName:chanelName
					}]);
				}
			}
			initChart3();
			initChart4();
		});
}
function initChart1(){
	var data = {
			name:'保护动作时间——历史趋势',
			xData:{
				name:'',
				value:[]
			},
			yData:{
				name:'毫秒',
				value:[]
			}
	};
	var length = 0;
	actTimelags.each(function(index,key,value){
		data.yData.value.push({
			lineName:key,
			lineValue:value
		});
		if(value.length > length){
			length = value.length;
		}
	});
	var selectProtectName = actTimes.keys[0];
	data.xData.value = actTimes.get(selectProtectName);
	
	var line = new CategoryLine('protectAct');
	line.setData(data);
	
	var option = line.getOption();
	option.legend.left = 230;
	option.legend.width = 800;
	option.legend.selectedMode = 'single';
	option.legend.top = 20;
	option.legend.selected[selectProtectName] = true;
	option.grid.top = 90;
	
	line.show();
	line.getChart().on('legendselectchanged',function(params){
		line.getChart().setOption({
			xAxis:{
				data:actTimes.get(params.name)
			}
		});
	});
	return line.getChart();
}
function initChart2(){
	var data = {
			name:'各保护动作时间——平均值',
			xData:{
				name:'保护名',
				value:[]
			},
			yData:{
				name:'毫秒',
				value:[{
					lineName:'保护平均动作时间',
					lineValue:[]
				}]
			}
	};
	actTimelags.each(function(index,key,value){
		var i=0;
		data.xData.value.push(key);
		var sum = 0;
		for(i in value){
			sum = sum + value[i];
		}
		data.yData.value[0].lineValue.push(sum/value.length);
	});
	
	var line = new CategoryLine('protectActAve');
	line.setData(data);
	
	var option = line.getOption();
	option.xAxis.axisLabel.rotate = 15;
	option.legend.top = 20;
	
	line.show();
	return line.getChart();
}
function initChart3(){
	var bar = new CategoryBar('alarmInfo');
	bar.setTitle('保护通道告警——异常次数');
	
	var ProtectsHasChanel = [];
	chanels.each(function(index,key,value){
		if(value.length>0){
			ProtectsHasChanel.push(key);
		}
	});
	
	bar.setxAxis(ProtectsHasChanel);
	bar.setxAxisTitle('保护名');
	//单通道频繁告警
	var singleAlarm = [];
	//双通道同时或相继告警
	var doubleAlarm = [];
	for(var i in ProtectsHasChanel){
		var protect = ProtectsHasChanel[i];
		singleAlarm.push(getSingleAlarmNum(protect));
		doubleAlarm.push(getDoubleAlarmNum(protect));
	}
	bar.setyAxisTitle('次数');
	bar.setyAxis(singleAlarm);
	bar.setyAxisName("单通道频繁告警");
	bar.setyAxis(doubleAlarm);
	bar.setyAxisName("双通道同时或相继告警");
	
	bar.show();
	return bar.getChart();
}
function initChart4(){
	var data = {
			name:'保护通道历史——告警次数',
			xData:{
				name:'保护名',
				value:[]
			},
			yData:{
				name:'次数',
				value:[{
					lineName:'紧急告警',
					lineValue:[]
				},{
					lineName:'主要告警',
					lineValue:[]
				},{
					lineName:'次要告警',
					lineValue:[]
				}]
			}
	};
	var chanelNames = [];
	chanels.each(function(index,key,value){
		chanelNames = chanelNames.concat(value);
	});
	data.xData.value = chanelNames;
	var jjvalue = [];
	var zyvalue = [];
	var cyvalue = [];
	var i = 0;
	for(i in chanelNames){
		chanelAlarms = alarms.get(chanelNames[i]);
		jjvalue[i] = 0;
		zyvalue[i] = 0;
		cyvalue[i] = 0; 
		for(var j in chanelAlarms){
			if(chanelAlarms[j].level == "紧急告警"){
				jjvalue[i] ++;
			}
			if(chanelAlarms[j].level == "主要告警"){
				zyvalue[i] ++;
			}
			if(chanelAlarms[j].level == "次要告警"){
				cyvalue[i] ++;
			}
		}
	}
	data.yData.value[0].lineValue = jjvalue;
	data.yData.value[1].lineValue = zyvalue;
	data.yData.value[2].lineValue = cyvalue;

	var bar = new CategoryBar('alarmLevel');
	bar.setData(data);
	bar.show();
	return bar.getChart();
}
/**
 * 返回一个保护的单通道频繁告警次数
 * 对于一个通道的全部告警，如果第二次告警距第一次告警小于半小时，第三次告警距第二次告警小于半小时，以此类推。共记为此通道对应的保护的一次频繁告警。
 * @param protect
 */
function getSingleAlarmNum(protect){
	var chas = chanels.get(protect);
	//一个数组，顺序为chanels的排序。每一项内是每个chanel的所有告警时间的数组
	var result = 0;
	for(var i in chas){
		var chanel = chas[i];
		var chanelAlarms = alarms.get(chanel);
		chanelAlarms.sort(compareAlarm);
		for(var j=0;j<chanelAlarms.length-1;){
			var time1;
			var time2;
			var suredo = false;
			//直到两个之间大于半小时
			do{
				time1 = chanelAlarms[j].triggerTime;
				j++;
				time2 = chanelAlarms[j].triggerTime;
				var lag = time2-time1;
				if(lag < 30*60*1000){
					suredo = true;
				}
			}while(lag < 30*60*1000 && j < chanelAlarms.length-1);
			if(suredo){
				result ++;
			}
		}
	}
	return result;
}
/**
 * 返回双通道同时或相继告警次数
 * 对于一个保护的全部告警，如果第二次告警距第一次告警小于半小时，第三次告警距第二次告警小于半小时，以此类推，并且告警通道不唯一。共记为此保护一次同时或相继告警。
 * @param protect
 */
function getDoubleAlarmNum(protect){
	var chas = chanels.get(protect);
	//一个数组，顺序为chanels的排序。每一项内是每个chanel的所有告警时间的数组
	var result = 0;
	var chanelAlarms = [];
	for(var i in chas){
		var chanel = chas[i];
		chanelAlarms.concat(alarms.get(chanel));
	}
	chanelAlarms.sort(compareAlarm);
	for(var j=0;j<chanelAlarms.length-1;){
		var time1;
		var time2;
		var suredo = false;
		do{
			time1 = chanelAlarms[j].triggerTime;
			chanel1 = chanelAlarms[j].chanelName;
			j++;
			time2 = chanelAlarms[j].triggerTime;
			chanel2 = chanelAlarms[j].chanelName;
			if(time2 - time1 < 30*60*1000 && !chanel1.equals(chanel2)){
				suredo = true;
			}
		}while(time2 - time1 < 30*60*1000 && j < chanelAlarms.length-1);
		if(suredo){
			result ++;
		}
	}
	return result;
}
/**
 * 对于告警信息进行排序
 * @param obj1
 * @param obj2
 */
function compareAlarm(obj1,obj2){
	if(obj1.triggerTime == undefined){
		return 1;
	}
	if(obj2.triggerTime == undefined){
		return -1;
	}
	if(obj1.triggerTime < obj2.triggerTime){
		return -1;
	}else{
		return 1;
	}
}


$("#alarmDiv").on('click', function (e) {
	var width = $('#protect').css("width");
	width = parseInt(width.substr(0,width.length-2))-35;
	$('#protectAlarmDiv').css("width",width+'px');
	$('#alarmLevel').css("width",width+'px');
	$('#alarmInfo').css("width",width+'px');
	initChart4().resize();
	initChart3().resize();
  });
$(function(){  
	lineId = $('#lineId').val();
	getData();
}); 